package com.mark.exception;

import com.mark.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Mark on 2018/4/21.
 * SpringMVC全局异常处理类
 */

@Slf4j
public class ExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.error("The Request:{} Exception:--> ",request.getRequestURI(),ex);
        ModelAndView modelAndView = new ModelAndView(new MappingJacksonJsonView());//注意：当使用是jackson2.x的时候使用MappingJackson2JsonView
        modelAndView.addObject("status", ResponseCode.Exception.getCode());
        modelAndView.addObject("msg","接口异常，请联系后端维护人员");
        modelAndView.addObject("data",ex.toString());
        return modelAndView;
    }
}
